import {
	captchaSms
} from '@/api/login/login'
import {
	toast
} from '@/utils/common'
import {
	getWxAppId
} from '@/api/sr/wechat'
import {
	AppType
} from '@/utils/constant.js'
export default {
	data() {
		return {
			title: '欢迎回来！', //填写logo或者app名称，也可以用：欢迎回来，看您需求
			second: 60, //默认60秒
			showText: true, //判断短信是否发送
			phone: '', //手机号码
			yzm: '' //验证码
		};
	},
	computed: {
		appMp() {
			return uni.getStorageSync('appType') == AppType.MP
		},
		appMINI() {
			return uni.getStorageSync('appType') == AppType.MINI
		}
	},
	methods: {
		//当前登录按钮操作
		login() {
			var that = this;
			if (!that.phone) {
				uni.showToast({
					title: '请输入手机号',
					icon: 'none'
				});
				return;
			}
			if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(that.phone)) {
				uni.showToast({
					title: '请输入正确手机号',
					icon: 'none'
				});
				return;
			}
			if (!that.yzm) {
				uni.showToast({
					title: '请输入验证码',
					icon: 'none'
				});
				return;
			}
			const params = {
				phoneNumber: this.phone,
				smsCode: that.yzm
			}
			//....此处省略，这里需要调用后台验证一下验证码是否正确，根据您的需求来
			this.$store.dispatch('SmsLogin', params).then(() => {
				this.$modal.closeLoading()
				this.loginSuccess()
				uni.showToast({
					title: '登录成功！',
					icon: 'none'
				});
			}).catch(() => {})
		},
		// 登录成功后，处理函数
		loginSuccess(result) {
			// 设置用户信息
			this.$store.dispatch('GetInfo').then(res => {
				this.$tab.reLaunch('/pages/user/index')
			}).catch(() => {})
		},
		//获取短信验证码
		getCode() {
			var that = this;
			if (!that.phone) {
				uni.showToast({
					title: '请输入手机号',
					icon: 'none'
				});
				return;
			}
			if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(that.phone)) {
				uni.showToast({
					title: '请输入正确手机号',
					icon: 'none'
				});
				return;
			}
			var interval = setInterval(() => {
				that.showText = false;
				var times = that.second - 1;
				//that.second = times<10?'0'+times:times ;//小于10秒补 0
				that.second = times;
				console.log(times);
			}, 1000);
			setTimeout(() => {
				clearInterval(interval);
				that.second = 60;
				that.showText = true;
			}, 60000);
			//这里请求后台获取短信验证码
			captchaSms({
				phoneNumber: this.phone
			}).then(response => {
				uni.showToast({
					title: '验证码：' + response.data,
					icon: 'none'
				});
			}).catch(errors => {})
		},
		toWeChatLogin() {
			const appType = uni.getStorageSync('appType')
			console.info(appType)
			if (appType !== AppType.MP && appType !== AppType.MINI) {
				return
			}
			console.info(uni.getStorageSync('isLogin'))
			// if (uni.getStorageSync('isLogin')) {
			// 	return;
			// }
			// uni.setStorageSync('isLogin', true)
			if (appType === AppType.MINI) {
				// 微信小程序
				// 请求微信接口获取 code
				wx.login({
					success: (res) => {
						// 用code 请求登录
						console.info(res)
						this.$store.dispatch('XcxLogin', res.code).then(() => {
							this.$modal.closeLoading()
							// 设置用户信息
							this.$store.dispatch('GetInfo').then(res => {
								this.$store.dispatch('GetBalance');
								this.$store.dispatch('GetCard');
								this.$tab.reLaunch('/pages/user/index')
							}).catch(() => {})
						}).catch(() => {
							window.location.href = window.location.origin + '#/pages/login/login'
						})
					}
				})
				return
			}
			if (appType === AppType.MP) {
				this.weChatLogin();
			}
		},
		weChatLogin() {
			const url = window.location.href
			if (url.indexOf('code') > -1) {
				uni.setStorageSync('isLogin', false)
				if (getApp().globalData.isLanding) return
				getApp().globalData.isLanding = true
				const code = url.split('code=')[1].split("&")[0];
				this.$store.dispatch('MpLogin', code).then(() => {
					this.$modal.closeLoading()
					// 设置用户信息
					this.$store.dispatch('GetInfo').then(res => {
						this.$tab.reLaunch('/pages/user/index')
					}).catch(() => {})
				}).catch(() => {
					window.location.href = window.location.origin + '#/pages/login/login'
				})
			} else {
				getWxAppId().then(res => {
					const redirectUrl = window.location.href;
					let param = 'appid=' + res.data;
					param += '&response_type=code'
					param += '&scope=snsapi_base'
					param += '&redirect_uri=' + encodeURIComponent(redirectUrl)
					param += '&state=bihuashop#wechat_redirect'
					window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?' + param
				}).catch(err => {
					window.location.href = window.location.origin + '#/pages/login/login'
				})
			}
		},
		goPage(url) {
			this.$tab.navigateTo(url)
		},
		passwordLogin() {
			this.$tab.navigateTo('/pages/login/passwordLogin')
		},
		todo() {
			toast('开发中');
		},
	}
};